package com.example.demo;

import android.content.Intent;
import android.os.Parcelable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.ScrollingTabContainerView;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    ArrayList<String> provinceNameList = new ArrayList<String>();

    ArrayList<String> cityNameList = new ArrayList<String>();

    ArrayList<String> areaNameList = new ArrayList<String>();

    Spinner provinceSpinner;

    Spinner citySpinner;

    Spinner areaSpinner;


    String jsonStr = "[\n" +
            "    {\"name\": \"jadyli\", \"gender\": \"male\"},\n" +
            "    {\"name\": \"sanqi\", \"gender\": \"male\"}\n" +
            "]";
    Button button;

    TextView showArea;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        provinceSpinner = findViewById(R.id.spinner_province);

        citySpinner = findViewById(R.id.spinner_city);

        areaSpinner = findViewById(R.id.spinner_area);

        button = findViewById(R.id.ok_btn);

        showArea = findViewById(R.id.show_areas);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String userProvince = (String) provinceSpinner.getSelectedItem();
                String userCity = (String) citySpinner.getSelectedItem();
                String userArea = (String) areaSpinner.getSelectedItem();

                Intent intent = new Intent(MainActivity.this,showAdressActivity.class);
                intent.putExtra("UP",  userProvince);
                intent.putExtra("UC",  userCity);
                intent.putExtra("UA",  userArea);
                startActivity(intent);
            }
        });

        loadData();

        findViewById(R.id.btn_1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    JSONArray jsonArray = new JSONArray(jsonStr);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        String gender = jsonObject.getString("gender");
                        String name = jsonObject.getString("name");

                        Log.e("MainActivity", "name=" + name + "gender=" + gender);

                        showArea.setText("name=" + name + "gender=" + gender);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        final Student s1 = new Student("张三", 20, "1713150");
        final Student s2 = new Student("张四", 21, "1713151");
        final Student s3 = new Student("张五", 22, "1713152");
        final ArrayList<Student> studentArrayList = new ArrayList<Student>();
        studentArrayList.add(s1);
        studentArrayList.add(s2);
        studentArrayList.add(s3);


        findViewById(R.id.btn_2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JSONObject jsonObject = new JSONObject();

                try {
                    jsonObject.put("name", s1.getName());
                    jsonObject.put("name", s1.getAge());
                    jsonObject.put("name", s1.getStuNo());

                    Log.e("MainActivity", "jsonObject=" + jsonObject);

                    showArea.setText("jsonObject=" + jsonObject);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
        findViewById(R.id.btn_3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JSONArray jsonArray = new JSONArray();
                for (Student student : studentArrayList) {
                    JSONObject jsonObject = new JSONObject();
                    try {
                        jsonObject.put("name", student.getName());
                        jsonObject.put("age", student.getAge());
                        jsonObject.put("stuNo", student.getStuNo());
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    jsonArray.put(jsonObject);
                }
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("studList", jsonArray);

                    JSONArray jsonArray1 = new JSONArray();
                    jsonArray1.put(jsonObject);
                    Log.e("MainActivity", "jsonObject=" + jsonObject.toString());
                    Log.e("MainActivity", "jsonArray=" + String.valueOf(jsonArray1));

                    showArea.setText("jsonObject=" + jsonObject.toString()+"\n"+
                            "jsonArray=" + String.valueOf(jsonArray1)
                );

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });


        findViewById(R.id.btn_4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    InputStream inputStream = getResources().getAssets().open("Data.txt");

                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

                    StringBuffer stringBuffer = new StringBuffer();

                    String line = bufferedReader.readLine();

                    while (line != null) {
                        stringBuffer.append(line);
                        line = bufferedReader.readLine();

                    }
                    JSONArray provinceJsonArray = new JSONArray(stringBuffer.toString());

                    for (int i = 0; i < provinceJsonArray.length(); i++) {
                        JSONObject provinceJsonObject = provinceJsonArray.getJSONObject(i);
                        String provinceName = provinceJsonObject.getString("name");
                        provinceNameList.add(provinceName);
                        /*Log.e("MainActivity","provinceName="+provinceName);*/

                        if (provinceName.equals("广东省")) {
                            JSONArray cityJsonArray = provinceJsonObject.getJSONArray("city");
                            for (int j = 0; j < cityJsonArray.length(); j++) {
                                JSONObject cityObject = cityJsonArray.getJSONObject(j);
                                String cityName = cityObject.getString("name");


                                if (cityName.equals("广州市")) {
                                    JSONArray areajsonArray = cityObject.getJSONArray("area");
                                    for (int k = 0; k < areajsonArray.length(); k++) {
                                        String areaName = areajsonArray.getString(k);
                                        Log.e("MainActivity", "area=" + areaName);                                    }
                                }
                            }
                        }
                    }

                    /*Log.e("MainActivity","stringJson="+stringBuffer.toString());*/
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void loadData() {
        //加载Data.txt文件：
        InputStream inputStream = null;
        try {
            inputStream = getResources().getAssets().open("Data.txt");

            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

            StringBuffer stringBuffer = new StringBuffer();

            String line = bufferedReader.readLine();

            while (line != null) {
                stringBuffer.append(line);
                line = bufferedReader.readLine();

            }
            final JSONArray provinceJsonArray = new JSONArray(stringBuffer.toString());

            for (int i=0;i<provinceJsonArray.length();i++){
                JSONObject provinceJsonObject = provinceJsonArray.getJSONObject(i);
                String provinceName = provinceJsonObject.getString("name");
                provinceNameList.add(provinceName);
            }
            //设置省份适配器
            ArrayAdapter<String> adapterProvince = new ArrayAdapter<>(MainActivity.this,R.layout.item,R.id.textview,provinceNameList);
            adapterProvince.setDropDownViewResource(R.layout.item2);
            provinceSpinner.setAdapter(adapterProvince);
            provinceSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    try {
                        JSONObject provinceJsonObjectSel = provinceJsonArray.getJSONObject(position);

                        final JSONArray cityJsonArray = provinceJsonObjectSel.getJSONArray("city");
                        if (cityNameList!=null){
                            cityNameList.clear();
                        }
                        for (int j=0;j<cityJsonArray.length();j++){
                            JSONObject cityJsonObject = cityJsonArray.getJSONObject(j);
                            String cityName = cityJsonObject.getString("name");

                            cityNameList.add(cityName);
                        }
                       ArrayAdapter<String> adapterCity = new ArrayAdapter<>(MainActivity.this,R.layout.item,R.id.textview,cityNameList);
                       adapterCity.setDropDownViewResource(R.layout.item2);
                       citySpinner.setAdapter(adapterCity);
                       citySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                           @Override
                           public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                               try {
                                   JSONObject cityJsonObject = cityJsonArray.getJSONObject(position);

                                   JSONArray areaJsonArray = cityJsonObject.getJSONArray("area");

                                   if (areaNameList!=null){
                                       areaNameList.clear();
                                   }
                                   for (int k=0;k<areaJsonArray.length();k++){

                                       areaNameList.add(areaJsonArray.getString(k));
                                   }
                                   ArrayAdapter<String> areaAdapter = new ArrayAdapter<String>(MainActivity.this,R.layout.item,R.id.textview,areaNameList);
                                   areaAdapter.setDropDownViewResource(R.layout.item2);
                                   areaSpinner.setAdapter(areaAdapter);
                                   areaSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                                       @Override
                                       public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                                       }

                                       @Override
                                       public void onNothingSelected(AdapterView<?> parent) {

                                       }
                                   });
                               } catch (JSONException e) {
                                   e.printStackTrace();
                               }
                           }

                           @Override
                           public void onNothingSelected(AdapterView<?> parent) {

                           }
                       });
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });

        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }


    }


}
